package org.example.dic.core.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.api.dic.IDicServiceFeignClient;
import org.example.dic.core.entity.DicInfo;
import org.example.dic.core.form.InsertDicInfoForm;
import org.example.dic.core.form.UpdateDicInfoForm;
import org.example.dic.core.service.IDicInfoService;
import org.example.dic.core.service.IDicTypeService;
import org.example.dic.core.service.impl.DicCacheService;
import org.example.system.common.dto.dic.DicInfoDto;
import org.example.system.common.utils.ResponseUtil;
import org.example.system.common.vo.PageIntVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 数据字典信息表  前端控制器
 * </p>
 *
 * @author Joey
 * @since 2021-05-13
 */
@ApiSort(1)
@Api(tags = "1、字典管理")
@Slf4j
@RestController
public class DicInfoController implements IDicServiceFeignClient {
    @Autowired
    IDicInfoService dicInfoService;
    @Resource
    IDicTypeService dicTypeService;
    @Resource
    DicCacheService dicCacheService;

    @ApiOperationSupport(order = 101)
    @ApiOperation(value = "DIC101-新增字典信息")
    @PostMapping("/v1/dicInfo")
    public ResponseUtil insertDicType(InsertDicInfoForm form){
        if(!dicTypeService.existByTypeKey(form.getTypeKey(),null)){
            return ResponseUtil.error("类型key不存在");
        }
        if(dicInfoService.existByTypeKeyAndDicKey(form.getTypeKey(),form.getDicKey(),null)){
            return ResponseUtil.error("字典key已存在");
        }
        DicInfo dicInfo = new DicInfo();
        BeanUtil.copyProperties(dicInfo,form);
        dicInfo.setIdPath(dicInfoService.queryIdPath(dicInfo.getParentId()));
        dicInfoService.save(dicInfo);
        dicCacheService.dicInfoListCacheUpdate(form.getTypeKey());
        return ResponseUtil.success();
    }

    @ApiOperationSupport(order = 102)
    @ApiOperation(value = "DIC102-删除字典信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="dicInfoId",value="字典信息id",dataType = "long", paramType = "query",required = true)
    })
    @DeleteMapping("/v1/dicInfo")
    public ResponseUtil delDicType(@NotNull(message = "字典信息id不能为空") Long dicInfoId){
        DicInfo dicInfo = dicInfoService.getById(dicInfoId);
        if(dicInfo == null){
            return ResponseUtil.error("字典信息不存在");
        }
        dicInfoService.removeById(dicInfo.getId());
        dicCacheService.dicInfoListCacheUpdate(dicInfo.getTypeKey());
        return ResponseUtil.success();
    }
    @ApiOperationSupport(order = 103)
    @ApiOperation(value = "DIC103-修改字典信息")
    @PutMapping("/v1/dicInfo")
    public ResponseUtil updateDicType(UpdateDicInfoForm form){
        DicInfo dicInfo = dicInfoService.getById(form.getId());
        if(dicInfo == null){
            return ResponseUtil.error("字典信息不存在");
        }
        if(StrUtil.isNotEmpty(form.getDicKey()) && dicInfoService.existByTypeKeyAndDicKey(dicInfo.getTypeKey(),form.getDicKey(),dicInfo.getId())){
            return ResponseUtil.error("字典key已存在");
        }
        dicInfoService.updateDicInfoByForm(form);
        dicCacheService.dicInfoListCacheUpdate(dicInfo.getTypeKey());
        return ResponseUtil.success();
    }


    @ApiOperationSupport(order = 199)
    @ApiOperation(value = "DIC199-查询字典信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="typeKey",value="字典类型key",dataType = "string", paramType = "query",required = true)
    })
    @Override
    public ResponseUtil<PageIntVo<DicInfoDto>> queryDicInfoList(@NotNull(message = "字典类型key不能为空") String typeKey) {
        List<DicInfoDto> list = dicInfoService.queryDicInfoList(typeKey);
        return ResponseUtil.success(PageIntVo.create(list.size(),list));
    }

    @ApiOperationSupport(order = 198)
    @ApiOperation(value = "DIC198-查询字典信息文本")
    @ApiImplicitParams({
            @ApiImplicitParam(name="typeKey",value="字典类型key",dataType = "string", paramType = "query",required = true),
            @ApiImplicitParam(name="dicKeys",value="字典值key",dataType = "string", paramType = "query",required = true)
    })
    @Override
    public ResponseUtil<String> queryDicInfoText(
            @NotNull(message = "字典类型key不能为空") String typeKey,
            @NotNull(message = "字典值key不能为空") String dicKeys
    ) {
        return ResponseUtil.success(dicInfoService.queryDicTextByKeys(typeKey,dicKeys));
    }
}

